package cn.math.appmath.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import cn.math.appmath.bean.TrenderBean;
import cn.math.appmath.biz.TrenderBiz;
import cn.math.appmath.entity.Trender;

import com.opensymphony.xwork2.ActionSupport;

public class TrenderAction extends ActionSupport {
	private static final long serialVersionUID = -4565898601842133546L;
	private TrenderBiz trenderBiz;
	private TrenderBean trenderBean;
	private Trender trender;
	private String message;
	private Integer[] nos;
	
	//上传文件的files对象
	private File file;
	//上传文件名
	private String fileFileName;
	//上传文件的MIME类型
	private String fileContentType;
	//保存上传文件的目录，相对于Web应用程序的根路径，在struts.xml文件中配置
	private String uploadDir;
	
	public String findTrender(){
		int page = 0;
		String pageStr = ServletActionContext.getRequest().getParameter("page");
		String param = ServletActionContext.getRequest().getParameter("param");
		if(pageStr != null && !"".equals(pageStr))
			page = new Integer(pageStr);
		if(param == null || param.equals(""))
			param = "backward";
		trenderBean = trenderBiz.findAll(page, param);
		return SUCCESS;
	}

	public String findById(){
		Integer id = new Integer(ServletActionContext.getRequest().getParameter("id"));
		trender = trenderBiz.getById(id);
		return SUCCESS;
	}
	
	public String trenderforward(){
		message = "";
		return SUCCESS;
	}
	
	/**
	 * 后台添加动态信息
	 * @return
	 */
	public String saveTrender(){
		if(file != null){
			String newImageName = uploadImage();
			if(newImageName != null && !newImageName.equals("")){
				trender.setTpicture(uploadDir + "/" + newImageName);
			}
		}
		if(trenderBiz.save(trender)){
			message = "保存动态信息成功";
			return SUCCESS;
		}
		message = "保存动态信息失败";
		return ERROR;
	}
	
	/**
	 * 图片上传处理方法 
	 * @return
	 */
	private String uploadImage(){
		try{
			String newImageName = null;
			long now = new Date().getTime();
			
			//得到保存上传文件的目录的真实路径
			String path = ServletActionContext.getServletContext().getRealPath(uploadDir);
			
			File dir = new File(path);
			//如果这个目录不存在，则创建它
			if(!dir.exists())
				dir.mkdir();
			int index = fileFileName.lastIndexOf('.');
			//判断上传文件名是否有扩展名，以时间作为新的文件名
			if(index != -1)
				newImageName = now + fileFileName.substring(index);
			else
				newImageName = Long.toString(now);
			BufferedOutputStream bos = null;
			BufferedInputStream bis = null;
			
			//读取保存在临时目录下的上传文件、写入到新的文件中
			try{
				FileInputStream fis = new FileInputStream(file);
				bis = new BufferedInputStream(fis);
				
				FileOutputStream fos = new FileOutputStream(new File(dir, newImageName));
				bos = new BufferedOutputStream(fos);
				
				byte[] buf = new byte[4096];
				
				int len = -1;
				while((len = bis.read(buf)) != -1)
					bos.write(buf, 0, len);				
			}catch(IOException e){
				e.printStackTrace();
			}finally{
				try{
					if(null != bis)
						bis.close();
				}catch(IOException e){
					e.printStackTrace();
				}
				
				try{
					if(null != bos)
						bos.close();
				}catch(IOException e){
					e.printStackTrace();
				}
			}
			return newImageName;
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	
	public String deleteTrender(){
		String noStr = ServletActionContext.getRequest().getParameter("no");
		if(noStr != null && !"".equals(noStr)){
			Integer no = new Integer(noStr);
			nos = new Integer[]{no};
		}
		if(this.trenderBiz.delete(nos)){
			return SUCCESS;
		}else{
			message = "删除动态信息失败";
			return INPUT;
		}
	}
	
	public void setTrenderBiz(TrenderBiz trenderBiz) {
		this.trenderBiz = trenderBiz;
	}

	public TrenderBean getTrenderBean() {
		return trenderBean;
	}

	public void setTrenderBean(TrenderBean trenderBean) {
		this.trenderBean = trenderBean;
	}

	public Trender getTrender() {
		return trender;
	}

	public void setTrender(Trender trender) {
		this.trender = trender;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getUploadDir() {
		return uploadDir;
	}

	public void setUploadDir(String uploadDir) {
		this.uploadDir = uploadDir;
	}

	public Integer[] getNos() {
		return nos;
	}

	public void setNos(Integer[] nos) {
		this.nos = nos;
	}
}
